//
//  testFriendCell.m
//  NiubilityProject
//
//  Created by Apple on 2018/6/1.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "testFriendCell.h"
#import "DataModel.h"



@implementation testFriendCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)js_setupCell{
    
    _headericon = [YYAnimatedImageView new];
    _nameL = [YYLabel new];
    _nameL.kitManager.label_font(UIFontMake(14)).label_textColor(JS_colorWithHex(kColorBlack4));
    
    _contenL = [UILabel new];
    _contenL.kitManager.label_font(UIFontMake(13))
    .label_textColor(JS_colorWithHex(kColorGray6))
    .label_numberOfLines(0)
    .label_lineBreakMode(NSLineBreakByCharWrapping);
    
    _imageContenView = [imageContenView new];
    
    [self.contentView js_addSubviews:@[_headericon, _nameL, _contenL, _imageContenView]];
    
    [_headericon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(kRealValue(10));
        make.size.mas_equalTo(CGSizeMake(kRealValue(40), kRealValue(40)));
    }];
    
    [_nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headericon.mas_right).offset(kRealValue(10));
        make.centerY.equalTo(self.headericon);
    }];
    [_contenL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headericon);
        make.right.equalTo(self.contentView).offset(kRealValue(-10));
        make.top.equalTo(self.headericon.mas_bottom).offset(kRealValue(10));
    }];
    [_imageContenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headericon);
        make.top.equalTo(self.contenL.mas_bottom).offset(kRealValue(10));
        make.right.equalTo(self.contentView).offset(kRealValue(-10));
        make.bottom.equalTo(self.contentView).offset(kRealValue(-10));
    }];
    
}
- (void)setDataModel:(DataModel *)dataModel{
    _dataModel = dataModel;
    
    [self.headericon setImageURL:[NSURL URLWithString:dataModel.author.cover]];
    self.nameL.kitManager.label_text(dataModel.author.name);
    
    self.contenL.kitManager.label_text(dataModel.text);
    self.imageContenView.dataModel = dataModel;
    
}
- (CGSize)sizeThatFits:(CGSize)size{
    
    CGFloat totalHeight = 0;
    totalHeight += kRealValue(40); // 头像高度
    totalHeight += kRealValue(20); // 间距
    totalHeight += [self.contenL sizeThatFits:CGSizeMake(size.width - 2 * kRealValue(10), size.height)].height;
    totalHeight += [self.imageContenView sizeThatFits:size].height;
    totalHeight += kRealValue(10);
    return CGSizeMake(size.width, totalHeight);
}

@end

@implementation imageContenView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        NSMutableArray *picViews = [NSMutableArray new];
        for (int i = 0; i < 9; i++) {
            YYAnimatedImageView *imageView = [YYAnimatedImageView new];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.size = CGSizeMake(100, 100);
            imageView.hidden = YES;
            imageView.clipsToBounds = YES;
            imageView.exclusiveTouch = YES;
            [self addSubview:imageView];
            [picViews addObject:imageView];
//            imageView.touchBlock = ^(YYControl *view, YYGestureRecognizerState state, NSSet *touches, UIEvent *event) {
//                if (![weak_self.cell.delegate respondsToSelector:@selector(cell:didClickImageAtIndex:)]) return;
//                if (state == YYGestureRecognizerStateEnded) {
//                    UITouch *touch = touches.anyObject;
//                    CGPoint p = [touch locationInView:view];
//                    if (CGRectContainsPoint(view.bounds, p)) {
//                        [weak_self.cell.delegate cell:weak_self.cell didClickImageAtIndex:i];
//                    }
//                }
            };
        self.pics = picViews.copy;
    }
    return self;
}
- (void)setDataModel:(DataModel *)dataModel{
    _dataModel = dataModel;
    picInfo *pic = nil;
    for (int i = 0; i < 9; i++) {
        CGFloat w = 0;
        CGFloat h = 0;
        CGPoint origin = {0};
        YYAnimatedImageView *imageView = self.pics[i];
        if (i >= dataModel.pic.count) {
            [imageView cancelCurrentImageRequest];
            imageView.hidden = YES;
        } else {
            pic = self.dataModel.pic[i];
            if (dataModel.pic.count == 1) {
                
                origin.x = 0;
                origin.y = 0;
                w = 200;
                h = 200;
            }else if (dataModel.pic.count < 5){
                w = 120;
                h = w;
                origin.x = (i % 2) * (w + kRealValue(10));
                origin.y = (i / 2) * (w + kRealValue(10));
            }else{
                w = h = 80;
                origin.x = (i % 3) * (w + kRealValue(10));
                origin.y = (i / 3) * (w + kRealValue(10));
            }
        }
        imageView.frame = CGRectMake(origin.x, origin.y, w, h);
        imageView.hidden = NO;
        
        [imageView setImageWithURL:[NSURL URLWithString:pic.url] options:YYWebImageOptionSetImageWithFadeAnimation];
    }
    
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGFloat h = 0;
    if (self.dataModel.pic.count) {
        
        if (self.dataModel.pic.count == 1) {
            h = 200;
        }else if (self.dataModel.pic.count < 5){
            NSInteger count = self.dataModel.pic.count / 2 + (self.dataModel.pic.count % 2 ? 1 : 0);
            h = count * (120 + kRealValue(10));
        }else{
            h = (self.dataModel.pic.count / 3 + (self.dataModel.pic.count % 3 ? 1 : 0)) * (80 + kRealValue(10));
        }
    }
    return CGSizeMake(size.width, h);
}

@end




